const app = getApp()
const {
  processImageUrl
} = require('../../utils/image')
const request = require('../../utils/request.js');
Page({
  data: {
    userInfo: null,
    isLoggedIn: false,
    stats: {
      posts: 0,
      demands: 0,
      history: 0
    },
  },

  onLoad() {
    this.checkLoginStatus()
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.checkLoginStatus()
  },

  // 检查登录状态
  checkLoginStatus() {
    const userInfo = app.getGlobalUserInfo()
    if (userInfo) {
      userInfo.avatar = processImageUrl(userInfo.avatar)
      this.setData({
        userInfo,
        isLoggedIn: true
      })
      this.loadUserStats()
    }
  },

  // 加载用户统计数据
  loadUserStats() {
    // 模拟获取用户统计数据
    request.request({
      url: `${app.globalData.baseUrl}/api/user/stat`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${app.getGlobalToken()}`
      },
      success: (res) => {
        if (res.data.code === 200) {
          const map = res.data.data
          const stats = {
            posts: map["residue"],
            demands: map["demand"],
            history: map["record"]
          }
          this.setData({
            stats
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '评论失败',
          icon: 'success'
        })
      },
      complete: () => {
        //
      }
    })
  },

  // 跳转到我的发布
  goToMyPosts() {
    wx.navigateTo({
      url: '/pages/my-posts/my-posts'
    })
  },

  // 跳转到我的需求
  goToMyDemands() {
    wx.navigateTo({
      url: '/pages/my-demands/my-demands'
    })
  },

  // 跳转到我的订单
  goToMyOrders() {
    wx.navigateTo({
      url: '/pages/orders/orders'
    })
  },


  // 跳转到我的咨询
  goToMyConsultations() {
    wx.navigateTo({
      url: '/pages/my-consultations/my-consultations'
    })
  },

  // 跳转到我的足迹
  goToMyHistory() {
    wx.navigateTo({
      url: '/pages/my-history/my-history'
    })
  },

  // 跳转到修改密码页面
  goToChangePassword() {
    wx.navigateTo({
      url: '/pages/my/change-password/change-password'
    })
  },

  // 联系客服
  contactService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567',
      fail: () => {
        wx.showToast({
          title: '拨打电话失败',
          icon: 'none'
        })
      }
    })
  },

  // 跳转到关于我们
  goToAbout() {
    wx.showToast({
      title: '该功能正在开发，敬请期待。。。。。。。。。',
      icon: 'none'
    })
  },

  // 跳转到农余学堂管理
  goToAgriculturalResidue() {
    wx.navigateTo({
      url: '/pages/agricultural-residue/agricultural-residue'
    })
  },

  // 跳转到登录页面
  goToLogin() {
    if (!this.data.isLoggedIn) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    } else {
      wx.navigateTo({
        url: '/pages/my/profile/profile'
      });
    }
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除用户信息
          wx.removeStorageSync('userInfo')
          wx.removeStorageSync('token')
          this.setData({
            userInfo: null,
            isLoggedIn: false,
            stats: {
              posts: 0,
              demands: 0,
              history: 0
            },
          })
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
        }
      }
    })
  }
})